Microsoft Message Queue(MSMQ)


Introduction

This article describes how to access messages from message queues, and how to peek and receive messages from message queues.

Microsoft Message Queue Server (MSMQ) is Microsoft's message queuing server. MSMQ can be used as a message broker to transmit messages from one application to another.

Microsoft Message Queuing, or MSMQ, is technology for asynchronous messaging. Whenever there's need for two or more applications  to send messages to each other without having to immediately know results, MSMQ can be used. MSMQ can communicate between remote machines, even
over the internet. It's free and comes with Windows, but is not installed by default.

Install MSMQ our Machine

1. Go to Control Panel > Add/Remove Programs > Add/Remove Windows Components. I check "Message Queuing" and then click "Next".

MSMQ1.jpg

  • After this step is completed, you will be able to create a private or public  message queue. In a private Queue the computer is not integrated in the Active Directory. There are some limitations that exist in private queues, they are not published whereas public queues are.
     
  • Right click on my computer > Manage > Services and Applications

    MSMQ2.jpg

There are two types of Message Queues in MSMQ:
  1. Private Queues
  2. Public Queues.
Queue Type Message Send from Example
Public Queue Anywhere ServerName\QueueName 
Or
FormatName:DIRECT=TCP:01.10.10.00\Queue Name
Private Queue Same server .\private$\QueueName

1. Create Private and Public Queue
  • Right-click on private and public Queues >New > Private Queue /public queue

    MSMQ3.jpg

    MSMQ4.jpg
     

Message

When using MSMQ with .NET, a message can be any serializable object. When serializing a message to MSMQ, you can use different formatters (including Binary and XML). You can specify the formatting of your message through the message object that you are sending.

Code Snippet: Sending a Message:

Namespace- using System.Messaging

MessageQueue MyMessageQ = new MessageQueue();
Message MyMessage = new Message();
String strPath = .\private$\QueueName
MyMessageQ = new MessageQueue(strPath);
MyMessage = new Message(txtMsg.Text);
MyMessageQ.Send(MyMessage);

Code Snippet: Receiving a message in the .NET service:

MessageQueue msgQ = new MessageQueue(strPath);
Message myMessage = new Message();
Message[] messages = msgQ.GetAllMessages();
foreach (System.Messaging.Message m in messages)
{
byte[] bytes = new byte[256];
m.BodyStream.Read(bytes, 0, 256);
System.Text.ASCIIEncoding ascii = new          System.Text.ASCIIEncoding();
ListBox1.Items.Add(ascii.GetString(bytes, 0, 256));
}


Reference

http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.aspx 
 

Up Next
    Ebook Download
    View all
    Learn
    View all